package com.ruslan.magaz.repository;


import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import com.ruslan.magaz.entity.Info;
import com.ruslan.magaz.entity.InfoName;
import com.ruslan.magaz.entity.User;


@Repository
public interface InfoRepository extends JpaRepository<Info, Integer> {

	@Query("SELECT i FROM Info i WHERE (?1) = i.user")
    Page<Info> findAllByUser(User user, Pageable page);

    @Query("SELECT i FROM Info i WHERE (?1) = i.user ORDER BY i.id")
    List<Info> findAllByUser(User user);
    
    @Query("SELECT count(i) FROM Info i WHERE i.user = (?1)")
    int countInfosForUser(User user);
    
    @Query("SELECT i FROM InfoName i")
    List<InfoName> findAllInfoNames();
    
    @Query("SELECT count(i) FROM Info i WHERE i.infoName = (?1) AND i.value = (?2)")
    int countSameInfo(InfoName infoName, String value);
    
    @Query("SELECT count(i) FROM Info i WHERE i.infoName = (?2) AND i.value = (?3) AND i.id != (?1)")
    int countIdenticalInfo(int id, InfoName infoName, String value);
    
    @Query("SELECT i FROM InfoName i WHERE i.id = (?1)")
    InfoName findOneInfoName(int id);
}
